/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JFrame;

import br.ufmg.dees.insane.model.disc.analysismodel.AnalysisModel;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.Controller;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog.MatrixViewTableDialog;
import br.ufmg.dees.insane.util.IVector;

/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/
public class SetModelReduceNodalForcesCommand implements Command {
	
	private LearnInterface parent;
	private FemModel fmodel;
	private CmdButton[][] nGButtons;
	private MatrixViewTableDialog dlg;

	
	public SetModelReduceNodalForcesCommand(JFrame parent) {
		this.parent = (LearnInterface)parent;
		
	}
	
	
	/** Execute method. */
	public void execute() {
		
		//Driver
		fmodel = parent.getView().getController().getModel();	
		((Controller)parent.getView().getController()).getDriver().reduceEquationsVector();
		AnalysisModel anl = fmodel.getGlobalAnalysisModel();
		
		//Number Format
		DecimalFormat fmt = new DecimalFormat();
        DecimalFormatSymbols fsym = new DecimalFormatSymbols();
        fsym.setDecimalSeparator('.');
        fmt.setDecimalFormatSymbols(fsym);
        fmt.applyPattern("0.000E00");
        
//      Create the Nodal vector for each element
        ((Controller)parent.getView().getController()).getDriver().numberElementEquations();
		((Controller)parent.getView().getController()).getDriver().reduceEquationsVector();
		int lines = ((Element)fmodel.getElementsList().get(0)).globalEquivalentForceVector().getSize();
		dlg = new MatrixViewTableDialog("Nodal Reduce Forces Vector",lines);
		ArrayList sel = this.parent.getView().getController().getSelection();
		ListIterator selLI = sel.listIterator();
		int c = 1;
		int size = sel.size();
		while(selLI.hasNext()){
			Element elm = (Element)selLI.next();
			CmdButton[][] nVector = new CmdButton[lines][1];
			Color cl = new Color(255*c/size, 255-(255*c/size), 140*c/size);
			ArrayList nodes = elm.getIncidence();
			ListIterator nodeLI = nodes.listIterator();
			int cont = 0;
			while(nodeLI.hasNext()){
				Node no = (Node)nodeLI.next();
					for (int k=0; k<6; k++) {
						if (anl.getValidEquation(k)) {
							if(elm.getReducedEquation(cont) > 0){
					            nVector[cont][0]=new CmdButton(String.valueOf(fmt.format(no.getForce(k))));
					            nVector[cont][0].setBackground(cl);
					            nVector[cont][0].setToolTipText("Equation ["+elm.getReducedEquation(cont)+"][1] of the element "+elm.getLabel()+"  Nodal Forces Vector");
				    			
					        }
					        else{
					            nVector[cont][0]= new CmdButton("X");
					            nVector[cont][0].setBackground(cl);
					            nVector[cont][0].setToolTipText("Equation ["+elm.getReducedEquation(cont)+"][1] of the element "+elm.getLabel()+"  Nodal Forces Vector");
				    			
					        }
							cont++;
					};
			}
		}
			String[] lineNames = new String[lines];
			for(int l=0; l<lines; l++)
				lineNames[l] = ""+elm.getReducedEquation(l);	
			
			String[] colNames = new String[1];
			for(int n=0; n<1; n++)
				colNames[n] = "Nodal Forces";	
			dlg.addOutputTable(elm.getLabel(),lineNames, colNames,nVector);
			c++;
		}
        
       	//int t = dlg.showDialog();	
//		Create the Nodal vector for model
		
		((Controller)parent.getView().getController()).getDriver().reduceEquationsVector();
		IVector fm = ((Controller)parent.getView().getController()).getDriver().reducedNodalForceVector();
		MatrixViewTableDialog dlg1 = new MatrixViewTableDialog("Model Reduce Nodal Forces Vector", fm.getSize());
		nGButtons= new CmdButton[fm.getSize()][1];
		
		int [] eqs = ((Controller)parent.getView().getController()).getDriver().getNodeEquations();
		int [] posEqs = new int[fm.getSize()];
		int cont=0;
		for (int k=0; k< eqs.length; k++){
			if(eqs[k]>0){
				 posEqs[cont] = eqs[k];
				 cont ++;
			}
		}
		String[] lineNames = new String[fm.getSize()];
		for(int i=0; i< posEqs.length; i++){
			   lineNames[i] = ""+posEqs[i];
		}
		String[] colNames = new String[1];
		for(int k=0; k<1; k++)
			colNames[k] = "Forces";	
		
		ArrayList modelNodes = fmodel.getNodesList();
		ListIterator mNodeLI = modelNodes.listIterator();
		int cont2 =0;
		while(mNodeLI.hasNext()){
		Node modelNo = (Node)mNodeLI.next();
			for (int k=0; k<6; k++) {
				if (modelNo.getEquation(k) > 0) {
					nGButtons[cont2][0]= new CmdButton(String.valueOf(fmt.format(modelNo.getForce(k)))); 
					nGButtons[cont2][0].setBackground(Color.YELLOW);
					nGButtons[cont2][0].setCommand(new ForcesDecompositionCommand(cont2, 0, dlg, dlg1));
					nGButtons[cont2][0].addActionListener(parent);
					nGButtons[cont2][0].setToolTipText("Decompose the element on the Equation ["+posEqs[cont2]+"][1] of the model Nodal Forces Vector");
			    
					cont2++;
				};
			}
		}
		
		dlg1.addOutputTable("Model",lineNames,colNames,nGButtons);
	
		//Dialog
		int par = fm.getSize();
		String model= "Model Reduce Nodal Forces";
		String elm =  "Element Reduce Nodal Forces";	
		ModelFrame f = new ModelFrame(par, dlg1, model, dlg, elm);
		f.setVisible(true);
		
		
	}
	
	
//*****************************************************************************
	
	/** Impossible to undo - not implemented. */
	public void undo() {
	}
	
//*****************************************************************************
	
	/** Impossible to redo - not implemented. */
	public void redo() {
	}
	
//*****************************************************************************
	

}

